package com.ccf.business.etl.flow;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.component.api.constants.ComponentCodeConstants;
import com.component.api.constants.ComponentConstants;
import com.component.api.model.dto.DatabaseParamDTO;
import com.component.api.model.flow.NodeInfo;
import com.component.api.model.param.TableColumn;
import com.service.database.config.DataBase;
import com.common.model.exception.ParamFomatException;
import com.service.database.config.DatabaseConfig;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 描述：
 *
 * @author xianggj
 * @Date 2021/11/30 14:45
 **/
public class DataInputParamHandler extends ParamHandler {


    /**
     * 整理参数
     * @return
     */
    @Override
    public Map<String, String> finishingParam() {
        Map<String, String> params = getParams();
        String sourceId = params.get(ComponentConstants.SOURCE_ID);
        DatabaseConfig byId = databaseClient.getConfig(sourceId, null);
        if (byId == null){
            throw new RuntimeException("数据源不存在");
        }
        //注意前后顺序
        if (DataBase.MYSQL.name().equals(byId.getType())){
            nodeDTO.setCode(ComponentCodeConstants.MYSQL_INPUT);
        } else if (DataBase.POSTGRESQL.name().equals(byId.getType())){
            nodeDTO.setCode(ComponentCodeConstants.PG_INPUT);
        } else if (DataBase.ORACLE.name().equals(byId.getType())){
            nodeDTO.setCode(ComponentCodeConstants.ORACLE_INPUT);
        }
        DatabaseParamDTO databaseParamDTO = DatabaseParamDTO.builder()
                .type(byId.getType().name())
                .driver(byId.getDriver())
                .userName(byId.getUserName())
                .pwd(byId.getUserPwd())
                .schema(byId.getSchema())
                .dbName(byId.getDbName())
                .tableName(params.get(ComponentConstants.TABLE_NAME))
                .url(byId.getUrl())
                .build();
        params.put(ComponentConstants.CONFIG, JSON.toJSONString(databaseParamDTO));
        String columns = params.get(ComponentConstants.IN_COLUMNS);
        String tableName = params.get(ComponentConstants.TABLE_NAME);
        List<TableColumn> columsList = new ArrayList<>();
        try {
            List<Map<String, Object>> tableDesc =
                    databaseClient.getTableDesc(sourceId, tableName);
            for (Map<String, Object> map : tableDesc) {
                TableColumn tableColumn = new TableColumn();
                BeanUtil.fillBeanWithMapIgnoreCase(map, tableColumn, true);
                columsList.add(tableColumn);
            }
        } catch (Exception e) {
            e.printStackTrace();
            throw new ParamFomatException("获取表详情失败");
        }
        if (StrUtil.isNotBlank(columns)){
            List<String> cols = StrUtil.split(columns, ',');
            columsList = columsList.stream().filter(e->cols.contains(e.getColumnName())).collect(Collectors.toList());
        }
        params.put(ComponentConstants.COLUMNS, JSON.toJSONString(columsList));
        return params;
    }

    /**
     * 复写这个方法
     * @return
     */
    @Override
    public NodeInfo get(){
        NodeInfo nodeInfo = NodeInfo.builder().name(nodeDTO.getName())
                .id(nodeDTO.getId()).rule(nodeDTO.getCode()).build();
        nodeInfo.setParam(analysis());
        //重设rule
        nodeInfo.setRule(nodeDTO.getCode());
        return nodeInfo;
    }


}
